home *** CD-ROM | disk | FTP | other *** search
- 1 rem**********************************
- 2 rem*this program will exchange binary
- 3 rem*hexadecimal and decimal notation
- 4 rem* lines 10 - 80 display the menu * and get the function number
- 5 rem**********************************
- 6 poke53281,7:n$="":e$=""
- 7 print"[147]"
- 8 print e$
- 10 print"chose a function 1-7[146]"
- 20 print"1 binary to decimal"
- 30 print"2 binary to hexadecimal"
- 40 print"3 hexadecimal to decimal"
- 50 print"4 hexadecimal to binary"
- 60 print"5 decimal to binary"
- 70 print"6 decimal to hexadecimal"
- 75 print"7 exit":input a:if(a>7)or(a<1)thene$="nonexistant function":goto 7
- 77 if a=7 then end
- 80 print"enter your number":e$=""
- 81 rem********************************* * the next few lines accept the
- 82 rem* value into a string variable * and send it to the subroutines
- 83 rem* for conversion *********************************
- 90 input n$:on a gosub 1000,200,1200,400,1300,1100
- 95 print"[147]the answer is "n$:print:print"press any key to go to menu"
- 96 getxz$:ifxz$=""then goto96
- 97 goto 7
- 200 gosub 1000
- 210 gosub 1100
- 220 return
- 400 gosub 1200
- 410 gosub 1300
- 420 return
- 1000 rem******************************
- 1005 rem*this subroutine converts
- 1010 rem* binary to decimal
- 1015 rem*****************************
- 1020 l=len(n$):dn=0:d$=""
- 1030 if l>16 then e$="data too large":goto 7
- 1035 forx=1 to l
- 1040 ifnot(mid$(n$,x,1)="0"ormid$(n$,x,1)="1")then e$="bad bin data":goto 7
- 1045 next
- 1050 for x=lto1 step-1
- 1055 ch$=mid$(n$,x,1)
- 1060 ch=0
- 1065 if ch$="1" then ch=1
- 1070 dn =dn+(2^(l-x)*ch)
- 1075 next x
- 1080 n$=str$(dn):forx=1 to len(n$)
- 1085 if not mid$(n$,x,1)=" "then d$=d$+mid$(n$,x,1)
- 1090 next:n$=d$:return
- 1100 rem********************************
- 1101 rem*this subroutine will convert
- 1102 rem*decimal to hexadecimal
- 1103 rem******************************
- 1104 l=len(n$)
- 1105 for x=1 to l:c$=mid$(n$,x,1)
- 1110 ifasc(c$)<48orasc(c$)>57thene$="illegal decimal data":goto 7
- 1115 next x:if val(n$)>65535then e$="number too large":goto 7
- 1120 d=val(n$):nd=1:n$=""
- 1125 if 16^nd>=d+1then goto 1135
- 1130 nd=nd+1:goto 1125
- 1135 for x=nd to 1 step-1
- 1145 c=int(d/16^(x-1))
- 1150 d=d-c*16^(x-1)
- 1155 if c=15 then n$=n$+"f"
- 1160 if c=14 then n$=n$+"e"
- 1165 if c=13 then n$=n$+"d"
- 1170 if c=12 then n$=n$+"c"
- 1175 if c=11 then n$=n$+"b"
- 1180 if c=10 then n$=n$+"a"
- 1185 if c<10 then n$=n$+str$(c)
- 1190 next x :d$="":for x=1 to len(n$)
- 1195 ifnotmid$(n$,x,1)=" "thend$=d$+mid$(n$,x,1)
- 1196 next:n$=d$:return
- 1200 rem*******************************
- 1201 rem* this subroutine will convert * hexadecimal to decimal
- 1202 rem*******************************
- 1204 forx=1 to len(n$):d$=mid$(n$,x,1)
- 1205 ifasc(d$)<48 or asc(d$)>70thene$="illegal hex data":goto 7
- 1206 ifasc(d$)<65andasc(d$)>57 then e$="illegal hexadecimal data" goto 7
- 1207 if len(n$)>4 then e$="number too large":goto 7
- 1208 next
- 1209 n=0:d$=""
- 1210 for x=0 to len(n$)-1
- 1215 ch$=mid$(n$,len(n$)-x,1)
- 1220 if ch$="f" then f=15
- 1225 if ch$="e" then f=14
- 1230 if ch$="d" then f=13
- 1235 if ch$="c" then f=12
- 1240 if ch$="b" then f=11
- 1245 if ch$="a" then f=10
- 1250 if asc(ch$)<58 then f=val(ch$)
- 1255 n=n+f*16^x
- 1260 next x
- 1265 n$=str$(n)
- 1270 for x=1 to len(n$)
- 1280 ifnot mid$(n$,x,1)=" "then d$=d$+mid$(n$,x,1)
- 1285 next:n$=d$:return
- 1299 rem******************************** * this subroutine will convert
- 1300 rem* decimal to binary *******************************
- 1301 forx=1 to len(n$)
- 1302 ifasc(mid$(n$,x,1))>57orasc(mid$(n$,x,1))<48then e$="bad dec data":goto7
- 1303 next:ifval(n$)>65535then e$="number too large":goto 7
- 1309 bc=0:c=val(n$):n$=""
- 1310 if 2^bc<c then bc=bc+1:goto 1310
- 1315 for x=0 to bc
- 1320 c$="1"
- 1325 if c<2^bc then c$="0"
- 1330 if c>=2^bc then c=c-2^bc
- 1335 bc=bc-1
- 1340 n$=n$+c$
- 1345 next x
- 1350 return
-